%sm_dewarp
t0=clock;
xs=0.324;
ys=0.309;
%im31
[dp_true1,dp_warp1,p_center1]=sm_getcalidata2('grid31ture.txt','grid31.txt',xs,ys);%,xpx,ypx,xL,yL);
[np1,tmp]=size(dp_true1);
p_warp1=dp_warp1+repmat(p_center1,np1,1);
%im32
[dp_true2,dp_warp2,p_center2]=sm_getcalidata2('grid31ture.txt','grid32.txt',xs,ys);%,xpx,ypx,xL,yL);
[np2,tmp]=size(dp_true2);
p_warp2=dp_warp2+repmat(p_center2,np2,1);
%im33
[dp_true3,dp_warp3,p_center3]=sm_getcalidata2('grid33ture.txt','grid33.txt',xs,ys);%xpx,ypx,xL,yL);
[np3,tmp]=size(dp_true3);
p_warp3=dp_warp3+repmat(p_center3,np3,1);
%im34
[dp_true4,dp_warp4,p_center4]=sm_getcalidata2('grid34ture.txt','grid34.txt',xs,ys);%xpx,ypx,xL,yL);
[np4,tmp]=size(dp_true4);
p_warp4=dp_warp4+repmat(p_center4,np4,1);
%im35
[dp_true5,dp_warp5,p_center5]=sm_getcalidata2('grid35ture.txt','grid35.txt',xs,ys);%xpx,ypx,xL,yL);
[np5,tmp]=size(dp_true5);
p_warp5=dp_warp5+repmat(p_center5,np5,1);
%im36
[dp_true6,dp_warp6,p_center6]=sm_getcalidata2('grid31ture.txt','grid36.txt',xs,ys);%xpx,ypx,xL,yL);
[np6,tmp]=size(dp_true6);
p_warp6=dp_warp6+repmat(p_center6,np6,1);

dp_true=[dp_true1;dp_true2;dp_true3;dp_true4;dp_true5;dp_true6];
dp_warp=[dp_warp1;dp_warp2;dp_warp3;dp_warp4;dp_warp5;dp_warp6];
p_warp=[p_warp1;p_warp2;p_warp3;p_warp4;p_warp5;p_warp6];
n_equ=5;
x_max=960;
y_max=1280;
A1=sm_bernstein2d3(dp_true,dp_warp,p_warp,x_max,y_max,n_equ);

p_dewarp=sm_ptdewarp(A1,p_warp,x_max,y_max,n_equ);
p_dewarp1=p_dewarp(1:np1,:);
p_dewarp2=p_dewarp(np1+1:np1+np2,:);
p_dewarp3=p_dewarp(np1+np2+1:np1+np2+np3,:);
p_dewarp4=p_dewarp(np1+np2+np3+1:np1+np2+np3+np4,:);
p_dewarp5=p_dewarp(np1+np2+np3+np4+1:np1+np2+np3+np4+np5,:);
p_dewarp6=p_dewarp(np1+np2+np3+np4+np5+1:np1+np2+np3+np4+np5+np6,:);
p_center1=p_dewarp1(1,:);
p_center2=p_dewarp2(1,:);
p_center3=p_dewarp3(1,:);
p_center4=p_dewarp4(1,:);
p_center5=p_dewarp5(1,:);
p_center6=p_dewarp6(1,:);
dp_dewarp1=p_dewarp1-repmat(p_center1,np1,1);
dp_dewarp2=p_dewarp2-repmat(p_center2,np2,1);
dp_dewarp3=p_dewarp3-repmat(p_center3,np3,1);
dp_dewarp4=p_dewarp4-repmat(p_center4,np4,1);
dp_dewarp5=p_dewarp5-repmat(p_center5,np5,1);
dp_dewarp6=p_dewarp6-repmat(p_center6,np6,1);
dp_dewarp=[dp_dewarp1;dp_dewarp2;dp_dewarp3;dp_dewarp4;dp_dewarp5;dp_dewarp6];
p_true1=dp_true1+repmat(p_center1,np1,1);
p_true2=dp_true2+repmat(p_center2,np2,1);
p_true3=dp_true3+repmat(p_center3,np3,1);
p_true4=dp_true4+repmat(p_center4,np4,1);
p_true5=dp_true5+repmat(p_center5,np5,1);
p_true6=dp_true6+repmat(p_center6,np6,1);
p_true=[p_true1;p_true2;p_true3;p_true4;p_true5;p_true6];
e=p_dewarp-p_true;
ee=sqrt(e(:,1).^2+e(:,2).^2);
emean=mean(ee);
estd=std(ee);
emin=min(ee);
emax=max(ee);
ev=[emean,estd,emin,emax]

A2=sm_bernstein2d3(dp_warp,dp_dewarp,p_dewarp,x_max,y_max,n_equ);

I_new1=sm_dewarpshow('dataset_grid\im_00031.jpeg',A2,p_warp1,p_dewarp1,dp_true1,n_equ);
I_new1=sm_dewarpshow('dataset_grid\im_00032.jpeg',A2,p_warp2,p_dewarp2,dp_true2,n_equ);
I_new1=sm_dewarpshow('dataset_grid\im_00033.jpeg',A2,p_warp3,p_dewarp3,dp_true3,n_equ);
I_new1=sm_dewarpshow('dataset_grid\im_00034.jpeg',A2,p_warp4,p_dewarp4,dp_true4,n_equ);
I_new1=sm_dewarpshow('dataset_grid\im_00035.jpeg',A2,p_warp5,p_dewarp5,dp_true5,n_equ);
I_new1=sm_dewarpshow('dataset_grid\im_00036.jpeg',A2,p_warp6,p_dewarp6,dp_true6,n_equ);

%I_new1=sm_imdewarp2('dataset_grid\im_00031.jpeg',A2,n_equ);
%I_new2=sm_imdewarp2('dataset_grid\im_00032.jpeg',A2,n_equ);
%I_new3=sm_imdewarp2('dataset_grid\im_00033.jpeg',A2,n_equ);
%I_new4=sm_imdewarp2('dataset_grid\im_00034.jpeg',A2,n_equ);
%I_new5=sm_imdewarp2('dataset_grid\im_00035.jpeg',A2,n_equ);
%I_new6=sm_imdewarp2('dataset_grid\im_00036.jpeg',A2,n_equ);

etime(clock,t0)